<template>
  <!--  添加角色或者更新角色信息-->
  <div>
    <el-dialog
        title="提示"
        :close-on-click-modal=false
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="用户名" prop="username" label-width="100px">
          <el-input v-model="editForm.username" autocomplete="off"></el-input>
          <el-alert
              title="初始密码为123456"
              :closable="false"
              type="info"
              style="line-height: 12px;"
          ></el-alert>
        </el-form-item>

        <el-form-item label="所在城市" prop="city" label-width="100px">
          <el-input v-model="editForm.city" auto-complete="off"></el-input>
        </el-form-item>

        <el-form-item label="邮箱" prop="email" label-width="100px">
          <el-input v-model="editForm.email" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="状态" prop="statu" label-width="100px">
          <el-radio-group v-model="editForm.statu">
            <el-radio :label="0">禁用</el-radio>
            <el-radio :label="1">正常</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {getUserById, save} from '@/api/sys/user'
import {isEmail} from '@/utils/validate'

export default {
  name: "addUser",
  data() {
    return {
      dialogVisible: false,
      editForm: {
        statu: 1
      },
      editFormRules: {
        username: [
          {required: true, message: '请输入用户名称', trigger: 'blur'}
        ],
        email: [
          {required: true, message: '请输入邮箱', trigger: 'blur'}, {
            validator: (rule, value, callback) => {
              if (!isEmail(value)) {
                callback(new Error('邮箱格式错误'))
              } else {
                callback()
              }
            },
            trigger: "blur"
          }
        ],
        statu: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ]
      },
    }
  },
  methods: {
    handleClose() {
      this.resetForm()
    },
    //重置表单
    resetForm() {
      this.editForm = {statu: 1}
      this.dialogVisible = false
    },
    //提交表单
    submitForm() {
      this.$refs.editForm.validate(valid => {
        if (valid) {
          const data = this.editForm
          save(data).then(res => {
            this.$emit("handle", res)
            this.resetForm()
          })
        }
      })
    },
    //打开对话框
    openDialog() {
      this.dialogVisible = true
    },
    //设置表单
    setEditForm(id) {
      getUserById(id).then(res => {
        console.log(res)
        this.editForm = res.data
      })
    }
  }
}
</script>

<style scoped>

</style>
